package com.rayo.accesscontrollercopy.module.main;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Message;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BasePresenter;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.db.dao.CardUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.CardUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserImpl;
import com.rayo.accesscontrollercopy.model.network.bean.OpenApi;
import com.rayo.accesscontrollercopy.model.service.NetWorkService;
import com.rayo.accesscontrollercopy.model.service.NetworkData;
import com.rayo.accesscontrollercopy.model.service.ReaderService;
import com.rayo.accesscontrollercopy.utils.NetWorkUtils;
import com.rayo.wallreader.data.KeyBasicInfo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class MainPresenter extends BasePresenter {
    private IMainView iMainView;
    private int[] imageResId;
    private ExecutorService fixedThreadPool;
    private static ReaderService mReaderService;
    private static NetWorkService mNetWorkService;
    private int imagePos = 0;

    private static final int INIT = 0;
    private static final int LOOP_IMAGE = 1;
    private static final int SHOW_BASIC_INFO = 2;
    private static final int SHOW_SNACKBAR = 3;
    private static final int ADD_BASIC_INFO = 4;
    private static final int OPEN = 5;
    private static final int SHOE_ACTION = 6;
    private static final int NETWORK_ONLINE = 7;
    private static final int NETWORK_OPEN_RELAY = 8;

    private static final int LOOP_TIME = 30 * 1000; //ms
    private static final int THREAD_COUNT = 1;

    private KeyBasicInfo mKeyBasicInfo;
//    private CardBasicInfo mCardBasicInfo;

    private KeyUserDao mKeyUserDao = new KeyUserImpl();
    private CardUserDao mCardUserDao = new CardUserImpl();

//    private void showKeyProKey(ProKeyInfo proKeyInfo) {
//        if (null == mKeyBasicInfo) return;
//        KeyType key = KeyType.fromInt(mKeyBasicInfo.getType());
//        StringBuilder sb = new StringBuilder(64);
//        switch (key) {
//            case EMERGENCY_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Emergency Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_issue_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getKeyIssueTime()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_start_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getEngineeringKeyStart()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_end_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getEngineeringKeyEnd()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case ENG_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Engineering Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_issue_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getKeyIssueTime()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_start_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getEngineeringKeyStart()));
//                sb.append("\n");
//                sb.append(reference.get().getString(R.string.key_end_time)+
//                        TimeFormatUtil.dateFromYMDHS(reference.get(), proKeyInfo.getEngineeringKeyEnd()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case READLOCKID_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Read LockId Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                sb.append(reference.get().getString(R.string.key_lock_id)+String.valueOf(DataFormatUtils.byteArrayToInt(proKeyInfo.getReadLockIdKey())));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case SETTING_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Setting Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case EVENT_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Event Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case READLOCKNUM_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Read Lock Serial Number Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                sb.append(reference.get().getString(R.string.key_lock_serial_number)+
//                        HexUtil.encodeHexStr(proKeyInfo.getReadLockSerialKey()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case LOSS_KEY:
//                obtainMessage(ADD_BASIC_INFO, "BlackList Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case SYSCODE_KEY:
//                obtainMessage(ADD_BASIC_INFO, "System-Code Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            case EMPTY_KEY:
//                obtainMessage(ADD_BASIC_INFO, "Empty Key").sendToTarget();
//                sb.append(reference.get().getString(R.string.key_time)+
//                        TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//                obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//                break;
//            default:
//                break;
//        }
//    }
//
//    private void showUserKey(UserKeyInfo userKeyInfo) {
//        if (null == mKeyBasicInfo) return;
//        KeyType key = KeyType.fromInt(mKeyBasicInfo.getType());
//        StringBuilder sb = new StringBuilder(64);
//        if (USER_KEY == key) {
//            obtainMessage(ADD_BASIC_INFO, "User Key").sendToTarget();
//            KeyUserData keyUserData = mKeyUserDao.findByUserid(String.valueOf(mKeyBasicInfo.getKeyId()));
//            if (keyUserData.getId() == 0) {
//                sb.append(reference.get().getString(R.string.key_user_name)+ reference.get().getString(R.string.unknow_user));
//            } else {
//                sb.append(NameFormatUtil.showName(keyUserData.getFname(), keyUserData.getLname()));
//            }
//            sb.append("\n");
//            sb.append(reference.get().getString(R.string.key_time)+
//                    TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//            sb.append("\n");
//            sb.append(reference.get().getString(R.string.key_issue_time)+
//                    TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyIssueTime()));
//            sb.append("\n");
//            sb.append(reference.get().getString(R.string.key_start_time)+
//                    TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyStartTime()));
//            sb.append("\n");
//            sb.append(reference.get().getString(R.string.key_end_time)+
//                    TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyEndTime()));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//    }
//
//    private void showKeyEvents(KeyEventInfo keyEventInfo) {
//        StringBuilder sb = new StringBuilder(64);
//        sb.append(reference.get().getString(R.string.main_key_events));
//        sb.append("\n");
//        sb.append(reference.get().getString(R.string.key_event_count)+
//                String.valueOf(keyEventInfo.getEventList().size()));
//        List<EventData> eventDatas = new ArrayList<>();
//        EventDao eventDao = new EventImpl();
//        if (keyEventInfo.getEventList().size() > 0) {
//            sb.append("\n");
//            for (int i = 0; i < keyEventInfo.getEventList().size(); i++) {
//                sb.append(String.valueOf(i + 1) + ".");
//                sb.append(keyEventInfo.getEventList().get(i).ToString());
//                sb.append("\n");
//                eventDatas.add(new EventData(keyEventInfo.getEventList().get(i).lockId, mKeyBasicInfo.getKeyId(),
//                        keyEventInfo.getEventList().get(i).event, 4, keyEventInfo.getEventList().get(i).time.getTime()));
//            }
//        }
//        obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        eventDao.addAll(eventDatas);
//    }
//
//    private void showKeyUpdate(UserKeyInfo userKeyInfo) {
//        if (null == mKeyBasicInfo) return;
//        StringBuilder sb = new StringBuilder(64);
//        sb.append(reference.get().getString(R.string.main_key_update));
//        sb.append("\n");
//        sb.append(reference.get().getString(R.string.key_time)+
//                TimeFormatUtil.dateFromYMDWHS(reference.get(), mKeyBasicInfo.getKeyTime()));
//        sb.append("\n");
//        sb.append(reference.get().getString(R.string.key_issue_time)+
//                TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyIssueTime()));
//        sb.append("\n");
//        sb.append(reference.get().getString(R.string.key_start_time)+
//                TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyStartTime()));
//        sb.append("\n");
//        sb.append(reference.get().getString(R.string.key_end_time)+
//                TimeFormatUtil.dateFromYMDHS(reference.get(), userKeyInfo.getKeyEndTime()));
//        obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        sb = new StringBuilder(64);
//        if (userKeyInfo.getDst() != null) {
//            sb.append(reference.get().getString(R.string.key_update_dst));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//        sb = new StringBuilder(64);
//        if (null != userKeyInfo.getOpens() && userKeyInfo.getOpens().size() > 0) {
//            sb.append(reference.get().getString(R.string.key_update_permissions));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//        sb = new StringBuilder(64);
//        if (null != userKeyInfo.getTimezoneInfo() && userKeyInfo.getTimezoneInfo().size() > 0) {
//            sb.append(reference.get().getString(R.string.key_update_timezone));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//        sb = new StringBuilder(64);
//        if (null != userKeyInfo.getCalendarInfo() && userKeyInfo.getCalendarInfo().size() > 0) {
//            sb.append(reference.get().getString(R.string.key_update_calender));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//    }
//
//    private void showUserCard(UserCardInfo userCardInfo) {
//        if (null == mCardBasicInfo) return;
//        CardTypeEnum cardTypeEnum = CardTypeEnum.fromInt(mCardBasicInfo.getCardType());
//        StringBuilder sb = new StringBuilder(64);
//        if (CardTypeEnum.USER_CARD == cardTypeEnum) {
//            obtainMessage(ADD_BASIC_INFO, "User Card").sendToTarget();
//            CardUserData cardUserData = mCardUserDao.findByUserId(mCardBasicInfo.getCardId());
//            if (cardUserData.getId() == 0) {
//                sb.append(reference.get().getString(R.string.key_user_name)+ reference.get().getString(R.string.unknow_user));
//            } else {
//                sb.append(NameFormatUtil.showName(cardUserData.getFname(), cardUserData.getLname()));
//            }
//            sb.append("\n");
//            sb.append(reference.get().getString(R.string.key_issue_time)+
//                    TimeFormatUtil.dateFromYMDHS(reference.get(), userCardInfo.getCardIssueTime()));
//            obtainMessage(ADD_BASIC_INFO, sb.toString()).sendToTarget();
//        }
//    }

//    private final BroadcastReceiver mReaderReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            final String action = intent.getAction();
//            final ReaderData readerData = (ReaderData) intent.getSerializableExtra(ReaderService.EXTRA_DATA);
//            if (readerData.getPort() == ReaderBiz.PLUG_IN_READER) return;
//            switch (action) {
//                case ReaderService.ACTION_READER_FIND_KEY:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOW_BASIC_INFO, true).sendToTarget();
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_connect_failed)).sendToTarget();
//                    } else {
//                        obtainMessage(SHOW_BASIC_INFO, true).sendToTarget();
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_info)).sendToTarget();
//                        mKeyBasicInfo = (KeyBasicInfo) readerData.getData();
//                        mCardBasicInfo = null;
//                    }
//                    break;
//                case ReaderService.ACTION_READER_FIND_CARD:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOW_BASIC_INFO, true).sendToTarget();
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.card_connect_failed)).sendToTarget();
//                    } else {
//                        obtainMessage(SHOW_BASIC_INFO, true).sendToTarget();
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.card_reading_info)).sendToTarget();
//                        mCardBasicInfo = (CardBasicInfo) readerData.getData();
//                        mKeyBasicInfo = null;
//                    }
//                    break;
//                case ReaderService.ACTION_READER_READ_PROKEY:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_failed)).sendToTarget();
//                    } else {
//                        showKeyProKey((ProKeyInfo) readerData.getData());
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_event)).sendToTarget();
//                    }
//                    break;
//                case ReaderService.ACTION_READER_READ_USERKEY:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_failed)).sendToTarget();
//                    } else {
//                        showUserKey((UserKeyInfo) readerData.getData());
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_event)).sendToTarget();
//                    }
//                    break;
//                case ReaderService.ACTION_READER_KEY_OPEN:
//                    if (readerData.getData() instanceof Integer) {
//                        if (ReaderBiz.ERROR_CONNECT == (Integer) readerData.getData()) {
//                            obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_no_permission)).sendToTarget();
//                        } else if (ReaderBiz.ERROR_USER_IN_BLACK == (Integer) readerData.getData()) {
//                            obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_in_blacklist)).sendToTarget();
//                        } else {
//
//                        }
//                    } else {
//                        obtainMessage(OPEN, true).sendToTarget();
//                    }
//                    break;
//                case ReaderService.ACTION_READER_READ_EVENT:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_reading_failed)).sendToTarget();
//                    } else {
//                        showKeyEvents((KeyEventInfo) readerData.getData());
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_update_info)).sendToTarget();
//                    }
//                    break;
//                case ReaderService.ACTION_READER_WRITE_USERKEY:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_update_failed)).sendToTarget();
//                    } else {
//                        showKeyUpdate((UserKeyInfo) readerData.getData());
//                        WebSocketHelp.getmInstance().updateUserFinish(mKeyBasicInfo.getKeyId(), 2);
//                    }
//                    break;
//                case ReaderService.ACTION_READER_READ_PROCARD:
//                    break;
//                case ReaderService.ACTION_READER_CARD_OPEN:
//                    if (readerData.getData() instanceof Integer) {
//                        if (ReaderBiz.ERROR_CONNECT == (Integer) readerData.getData()) {
//                            obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_no_permission)).sendToTarget();
//                        } else if (ReaderBiz.ERROR_USER_IN_BLACK == (Integer) readerData.getData()) {
//                            obtainMessage(SHOE_ACTION, reference.get().getString(R.string.key_in_blacklist)).sendToTarget();
//                        } else {
//
//                        }
//                    } else {
//                        obtainMessage(OPEN, true).sendToTarget();
//                    }
//                    break;
//                case ReaderService.ACTION_READER_READ_USERCARD:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.card_reading_failed)).sendToTarget();
//                    } else {
//                        showUserCard((UserCardInfo) readerData.getData());
//                    }
//                    break;
//                case ReaderService.ACTION_READER_WRITE_USERCARD:
//                    if (readerData.getData() instanceof Integer) {
//                        obtainMessage(SHOE_ACTION, reference.get().getString(R.string.card_update_failed)).sendToTarget();
//                    } else {
//                        showUserCard((UserCardInfo) readerData.getData());
//                        WebSocketHelp.getmInstance().updateUserFinish(mCardBasicInfo.getCardId(), 3);
//                    }
//                    break;
//                default:
//                    break;
//            }
//        }
//    };

    private final BroadcastReceiver mNetworkReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            final NetworkData networkData = (NetworkData) intent.getSerializableExtra(NetWorkService.EXTRA_DATA);
            switch (action) {
                case NetWorkService.ACTION_NETWORK_CONNECT:
                    obtainMessage(NETWORK_ONLINE, true).sendToTarget();
                    break;
                case NetWorkService.ACTION_NETWORK_DISCONNECT:
                    obtainMessage(NETWORK_ONLINE, false).sendToTarget();
                    break;
                case NetWorkService.ACTION_NETWORK_FAILED:
                    obtainMessage(NETWORK_ONLINE, false).sendToTarget();
                    break;
                case NetWorkService.ACTION_NETWORK_OPEN:
                    OpenApi openApi = (OpenApi) networkData.getData();
                    if (GlobalVariables.getInstance().getmSiteData().getLockid() == Integer.valueOf(openApi.getLockid()))
                        obtainMessage(NETWORK_OPEN_RELAY).sendToTarget();
                    break;
                case NetWorkService.ACTION_NETWORK_API_ERROR:
                    obtainMessage(NETWORK_ONLINE, false).sendToTarget();
                    break;
                default:
                    break;
            }
        }
    };

    private final static ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mReaderService = ((ReaderService.LocalBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mReaderService = null;
        }
    };

    private final static ServiceConnection mNetworkConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mNetWorkService = ((NetWorkService.LocalBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mNetWorkService = null;
        }
    };

    /**
     * @param context
     * @param iMainView
     */
    public MainPresenter(Context context, IMainView iMainView) {
        this.init(context);
        this.iMainView = iMainView;
//        this.mReaderService = new ReaderService();
//        Intent intentReader = new Intent(context, ReaderService.class);
//        this.reference.get().bindService(intentReader, mServiceConnection, Context.BIND_AUTO_CREATE);
//        this.reference.get().registerReceiver(mReaderReceiver, makeReaderUpdateIntentFilter());
        this.mNetWorkService = new NetWorkService();
        Intent intentNetwork = new Intent(context, NetWorkService.class);
        GlobalVariables.getInstance().setAndroidId(NetWorkUtils.getAndroidId(reference.get()));
        this.reference.get().bindService(intentNetwork, mNetworkConnection, Context.BIND_AUTO_CREATE);
        this.reference.get().registerReceiver(mNetworkReceiver, makeNetworkUpdateIntentFilter());
    }

    @Override
    protected void message(Message msg) {
        switch (msg.what) {
            case INIT:
                iMainView.setDateFormat(GlobalVariables.getInstance().getmSystemData().getDateFormat());
                break;
            case LOOP_IMAGE:
//                iMainView.updateImage((Integer) msg.obj);
                break;
            case SHOW_BASIC_INFO:
//                iMainView.isShowInfo((boolean) msg.obj);
                break;
            case ADD_BASIC_INFO:
//                iMainView.setKeyInfo((String) msg.obj);
                break;
            case OPEN:
//                iMainView.open((boolean) msg.obj);
                break;
            case SHOW_SNACKBAR:
                iMainView.showSnackBar((String) msg.obj);
                break;
            case SHOE_ACTION:
//                iMainView.setActionInfo((String) msg.obj);
                break;
            case NETWORK_ONLINE:
                iMainView.setNetWork((boolean) msg.obj);
                break;
            case NETWORK_OPEN_RELAY:
                mReaderService.remoteOpen();
                break;
            default:
                break;
        }
    }

    @Override
    public void initData() {
        imageResId = new int[]{R.drawable.bg_1, R.drawable.bg_2, R.drawable.bg_3, R.drawable.bg_4};
        initThread();
    }

    /**
     * 线程执行
     */
    public void initThread() {
        this.fixedThreadPool = Executors.newFixedThreadPool(THREAD_COUNT);
        postDelayed(mLoopImages, LOOP_TIME);
    }
    /**
     *
     */
    public void onDestroy() {
        if (null != this.fixedThreadPool) {
            try {
                this.fixedThreadPool.shutdown();
                this.fixedThreadPool.awaitTermination(0, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
//        if (null != mReaderService)
//            this.reference.get().unregisterReceiver(mReaderReceiver);
//        this.reference.get().unbindService(mServiceConnection);
        try {
            this.reference.get().unbindService(mNetworkConnection);
        }catch (Exception e){
    e.printStackTrace();
        }finally {
            if (null != mNetWorkService)
                this.reference.get().unregisterReceiver(mNetworkReceiver);
            removeCallbacksAndMessages(null);
        }
    }

    Runnable mLoopImages = new Runnable() {
        @Override
        public void run() {
            obtainMessage(LOOP_IMAGE, imageResId[imagePos]).sendToTarget();
            imagePos++;
            if (imagePos == imageResId.length) imagePos = 0;
            postDelayed(this, LOOP_TIME);
        }
    };

    private static IntentFilter makeReaderUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ReaderService.ACTION_READER_FIND_KEY);
        intentFilter.addAction(ReaderService.ACTION_READER_FIND_CARD);
        intentFilter.addAction(ReaderService.ACTION_READER_READ_PROKEY);
        intentFilter.addAction(ReaderService.ACTION_READER_READ_USERKEY);
        intentFilter.addAction(ReaderService.ACTION_READER_READ_EVENT);
        intentFilter.addAction(ReaderService.ACTION_READER_WRITE_USERKEY);
        intentFilter.addAction(ReaderService.ACTION_READER_READ_USERCARD);
        intentFilter.addAction(ReaderService.ACTION_READER_READ_PROCARD);
        intentFilter.addAction(ReaderService.ACTION_READER_WRITE_USERCARD);
        intentFilter.addAction(ReaderService.ACTION_READER_KEY_OPEN);
        intentFilter.addAction(ReaderService.ACTION_READER_CARD_OPEN);
        return intentFilter;
    }
    private static IntentFilter makeNetworkUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(NetWorkService.ACTION_NETWORK_CONNECT);
        intentFilter.addAction(NetWorkService.ACTION_NETWORK_DISCONNECT);
        intentFilter.addAction(NetWorkService.ACTION_NETWORK_FAILED);
        intentFilter.addAction(NetWorkService.ACTION_NETWORK_API_ERROR);
//        intentFilter.addAction(NetWorkService.ACTION_NETWORK_OPEN);
        return intentFilter;
    }

    public boolean isNewSystem() {
        if (0 == GlobalVariables.getInstance().getmSiteData().getLockid())
            return true;
        else
            return false;
    }

}
